home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / tex / dvi / dvipssrc.zoo / flib.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  9KB  |  302 lines

  1. /*
  2.  *   Here's some code to handle font libraries.  Not needed unless you are
  3.  *   running on a system that can't handle files well.  Not endorsed by
  4.  *   Tomas Rokicki or Radical Eye Software; use at your own risk.
  5.  */
  6. #ifdef FONTLIB
  7. #include "structures.h"
  8. #include "paths.h"
  9. extern FILE *search() ;
  10. extern char *newstring() ;
  11. extern shalfword pkbyte() ;
  12. extern void badpk() ;
  13. extern char *malloc() ;
  14. extern integer pkquad() ;
  15. extern int debug_flag ;
  16. extern char errbuf[] ;
  17. extern char *flipath ;
  18. extern char *fliname ;
  19. extern FILE *pkfile ;
  20. /*
  21.  * font library structures
  22.  */
  23. struct fli_entry {
  24.    unsigned long  offset;
  25.    char          *name;
  26. };
  27.  
  28. struct fli_size {
  29.    unsigned long     size;
  30.    halfword          entries;
  31.    struct fli_entry *entry;
  32. };
  33.  
  34. struct fli_lib {
  35.    char            *name;
  36.    FILE            *fp;
  37.    halfword         sizes;
  38.    struct fli_size *size;
  39.    struct fli_lib  *next;
  40. };
  41.  
  42. struct fli_lib *firstlib = NULL;
  43.  
  44. Boolean flib = 0;  /* non zero if reading a font library */
  45.  
  46. halfword
  47. pkdouble()
  48. {
  49.    register halfword i ;
  50.    i = pkbyte() ;
  51.    i = i * 256 + pkbyte() ;
  52.    return(i) ;
  53. }
  54. extern char name[] ;
  55. /*
  56.  *   fliload opens each font library, then reads in its
  57.  *   directory for later use.  Font libraries are left
  58.  *   open for later use by flisearch().
  59.  *   loadfont() does not close pkfile if it is a font 
  60.  *   library (flib non zero).
  61.  */
  62. void
  63. fliload()
  64. {
  65.    int i ;
  66.    halfword version1, version2;
  67.    Boolean needext;
  68.    char fontname[50]; 
  69.    char name[50] ;
  70.    char *fli;
  71.    unsigned long dpi;
  72.    halfword len, numsizes, numfonts;
  73.    struct fli_lib *lib=NULL, *next_lib=NULL;
  74.    struct fli_size *size;
  75.    struct fli_entry *entry;
  76.  
  77.    fli = fliname;
  78.  
  79.    while (*fli) {
  80.       /* get next font library name from fliname */
  81.       needext=1;
  82.       for (i=0; *fli && *fli!=PATHSEP; i++)
  83.          if ( (name[i] = *fli++) == '.')
  84.             needext=0;
  85.       name[i] = '\0';
  86.       if (*fli)
  87.          fli++;  /* skip PATHSEP */
  88.       if (*name) { 
  89.          /* got fli name, now search for it */
  90.          if (needext)
  91.             strcat(name,".fli");
  92.  
  93.          if ( (pkfile=search(flipath,name,READBIN)) != (FILE *)NULL ) {
  94.             /* for each font library */
  95.             for (i=0; i<4; i++) {
  96.               fontname[i] = pkbyte();  /* read header */
  97.             }
  98.             version1 = pkbyte();
  99.             version2 = pkbyte();
  100.             if (strncmp(fontname,"FLIB",4)!=0 || version1 != 2 || version2 != 0)
  101.                badpk("incorrect font library format");
  102.  
  103.             (void) pkdouble();       /* ignore directory length */
  104.             numsizes = pkdouble();   /* number of sizes */
  105.             numfonts = pkdouble();   /* number of fonts */
  106.             len = pkdouble();        /* length of comment */
  107.             for (i=0; i<len; i++)
  108.                (void)pkbyte();       /* skip comment */
  109. #ifdef DEBUG
  110.    if (dd(D_FONTS))
  111.       (void)fprintf(stderr,"Font library %s has %d font size%s, %d font%s\n",
  112.          name, numsizes , numsizes !=1 ? "s" : "", 
  113.          numfonts, numfonts!=1 ? "s" : "");
  114. #endif /* DEBUG */
  115.  
  116.             next_lib =  (struct fli_lib *)malloc(sizeof(struct fli_lib)); 
  117.             if (next_lib == (struct fli_lib *)NULL)
  118.                error("! out of memory while reading font library") ;
  119.             if (firstlib == (struct fli_lib *)NULL)
  120.                firstlib = next_lib;
  121.             else
  122.                lib->next = next_lib;
  123.             lib = next_lib;
  124.             size = (struct fli_size *)
  125.                         malloc(numsizes * sizeof(struct fli_size)); 
  126.             if (size == (struct fli_size *)NULL)
  127.                error("! out of memory while reading font library") ;
  128.             entry = (struct fli_entry *)
  129.                         malloc(numfonts * sizeof(struct fli_entry)); 
  130.             if (entry == (struct fli_entry *)NULL)
  131.                error("! out of memory while reading font library") ;
  132.             lib->name = newstring(name);
  133.             lib->fp = pkfile;         /* keep file open */
  134.             lib->sizes = numsizes;
  135.             lib->size = size;
  136.             lib->next = (struct fli_lib *)NULL;
  137.  
  138.             for ( ;numsizes>0; numsizes--, size++) { 
  139.                /* for each font size in this library */
  140.                (void)pkdouble();      /* length of size entry - ignore */
  141.                numfonts = pkdouble(); /* number of fonts */
  142.                dpi = pkquad();        /* DPI (fixed point 16.16) */
  143.  
  144. #ifdef DEBUG
  145.    if (dd(D_FONTS))
  146.       (void)fprintf(stderr,"Font library %s size %.5gdpi has %d font%s\n", 
  147.                   name, dpi/65536.0, numfonts, numfonts!=1 ? "s" : "");
  148. #endif /* DEBUG */
  149.                size->size    = dpi ;
  150.                size->entries = numfonts ;
  151.                size->entry   = entry ;
  152.                   for ( ;numfonts > 0; numfonts--, entry++) {
  153.                      /* read each entry */
  154.                      (void)pkquad();            /* ignore length of font */
  155.                      entry->offset = pkquad();  /* offset to font */
  156.                      len = pkbyte();            /* length of name */
  157.                      for (i=0; i<len; i++)
  158.                         fontname[i] = pkbyte();
  159.                      fontname[len] = '\0';
  160.                      entry->name = newstring(fontname);
  161.                   } /* end for numfonts>0 */
  162.             }  /* end for numsizes>0 */
  163.             /* keep font library open */
  164.          }  /* end if opened library */
  165.       } /* end if (*name) */
  166.    }
  167. }
  168.    
  169.  
  170. /*
  171.  *   flisearch searches all the font libraries for a PK font.
  172.  *   returns FILE pointer positioned to PK font in font library
  173.  */
  174. FILE *
  175. flisearch(n, dpi)
  176. char *n;
  177. halfword dpi;
  178. {
  179.    halfword dpi1, numsizes, numfonts;
  180.    struct fli_lib *lib=NULL;
  181.    struct fli_size *size;
  182.    struct fli_entry *entry;
  183.    int del ;
  184.    
  185.    if (firstlib == (struct fli_lib *)NULL)
  186.       return((FILE *)NULL);  /* return if no font libraries */
  187.  
  188. #ifdef DEBUG
  189.       if (dd(D_FONTS))
  190.          (void)fprintf(stderr,"Trying %s at %ddpi\n", n, dpi);
  191. #endif /* DEBUG */
  192.    for (lib = firstlib; lib != (struct fli_lib *)NULL; lib = lib->next ) {
  193.       /* for each font library */
  194.       numsizes = lib->sizes ;
  195.       size = lib->size ;
  196. #ifdef DEBUG
  197.       if (dd(D_FONTS))
  198.          (void)fprintf(stderr,"  Searching %s\n", lib->name);
  199. #endif /* DEBUG */
  200.       for (; numsizes>0; numsizes--, size++) { 
  201.          /* for each font size in this library */
  202.          dpi1 = (halfword)((size->size+32768L)/65536) ;
  203.          if ( dpi1 == dpi ) {
  204.             /* if correct size then search for font */
  205. #ifdef DEBUG
  206.             if (dd(D_FONTS))
  207.                (void)fprintf(stderr, "    Checking size %ddpi\n",dpi1);
  208. #endif /* DEBUG */
  209.             entry = size->entry ;
  210.             for (numfonts=size->entries ;numfonts > 0; numfonts--, entry++) {
  211.                if (strcmp(entry->name,n)==0) {
  212.                   /* if correct font name then seek it */
  213.                      pkfile = lib->fp;
  214.                      flib = 1 ; /* this is a font library - don't close it */
  215.                      (void)sprintf(name,"%s %s %ddpi",lib->name, n, dpi1) ;
  216.                      if ( fseek(pkfile,entry->offset,0) )
  217.                            badpk("couldn't seek font");
  218.                         /* make sure it is a PK font */
  219.                         if (pkbyte()==247) /* pre byte */
  220.                            if (pkbyte()==89) {  /* id byte */
  221.                               if ( fseek(pkfile,entry->offset,0) )
  222.                                  badpk("couldn't seek font");
  223.                               return(pkfile); /* found it */
  224.                            }
  225.                         sprintf(errbuf,"%s %s %ddpi isn't PK format, ignoring",
  226.                               lib->name, n, dpi1);
  227.                         error(errbuf);
  228.                } /* end if name correct */
  229.             } /* end for numfonts>0 */
  230.          }
  231.          else {
  232.             /* if not correct size then skip */
  233. #ifdef DEBUG
  234.       if (dd(D_FONTS))
  235.          (void)fprintf(stderr, "    Skipping size %ddpi\n", dpi1);
  236. #endif /* DEBUG */
  237.          }
  238.       }  /* end for numsizes>0 */
  239.    }
  240.    return((FILE *)NULL);
  241. }
  242.  
  243. /* parse the font library path, putting all directory names in path, 
  244.  * and all font library nam